package comu.hnevc.zh.testdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

import comu.hnevc.zh.testdemo.bean.AllCarPeccancy;
import comu.hnevc.zh.testdemo.bean.CarInfo;
import comu.hnevc.zh.testdemo.fragment.AccountManagerFragment;
import comu.hnevc.zh.testdemo.fragment.CarPeccancyFragment;
import comu.hnevc.zh.testdemo.fragment.DataAnalyeseFragment;
import comu.hnevc.zh.testdemo.fragment.IderFragment;
import comu.hnevc.zh.testdemo.fragment.LifeHelpFragment;
import comu.hnevc.zh.testdemo.fragment.QueryBusFragment;
import comu.hnevc.zh.testdemo.fragment.RoadconditionFragment;
import comu.hnevc.zh.testdemo.fragment.TrafficlightManagerFragment;
import comu.hnevc.zh.testdemo.fragment.UserCenterFragment;

public class MainActivity extends AppCompatActivity {
    public TextView allrecharge;
    public TextView person;
    public RequestQueue requestQueue;
    public Timer timer;
    public List<AllCarPeccancy> allCarPeccancieList;
    public List<CarInfo> carInfoList;
    SlidingPaneLayout mSlidingPaneLayout;
    TextView mTvTitle;
    ListView mListView;
    ImageView mIvHome;
    List<Map<String,Object>> menuList;
    boolean flag =false;
    String[] menuNameArray;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestQueue=Volley.newRequestQueue(this);
        timer=new Timer();
        initdata();
        initview();

    }
    private void initview() {
        //获取侧滑控件
        mSlidingPaneLayout=findViewById(R.id.spl_root);
        //获取home按键
        mIvHome=findViewById(R.id.header_back_iv);

        //标题部分
        mTvTitle=findViewById(R.id.header_title_tv);
        mListView=findViewById(R.id.id_lv_menu);
        final MyBaseAdapter baseAdapter=new MyBaseAdapter();
        mListView.setAdapter(baseAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSlidingPaneLayout.closePane();
                String name= (String) menuList.get(position).get("name");
                mTvTitle.setText(name);
                Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
                switch (name){
                    case "账户管理":
                        AccountManagerFragment accountManagerFragment=new AccountManagerFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,accountManagerFragment).commit();
                                break;
                    case"红绿灯管理":
                        TrafficlightManagerFragment trafficlightManagerFragment=new TrafficlightManagerFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,trafficlightManagerFragment).commit();
                                 break;
                    case "公交查询":
                        QueryBusFragment queryBusFragment=new QueryBusFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,queryBusFragment).commit();
                                 break;
                    case "车辆违章":
                        CarPeccancyFragment fragment=new CarPeccancyFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,fragment).commit();
                        break;
                    case "路况查询":
                        RoadconditionFragment roadconditionFragment=new RoadconditionFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,roadconditionFragment).commit();
                        break;
                    case "生活助手":
                        LifeHelpFragment lifeHelpFragment=new LifeHelpFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,lifeHelpFragment).commit();
                        break;
                    case"数据分析":
                        DataAnalyeseFragment dataAnalyeseFragment=new DataAnalyeseFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,dataAnalyeseFragment).commit();
                        break;
                    case"个人中心":
                        UserCenterFragment userCenterFragment=new UserCenterFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,userCenterFragment).commit();
                        break;
                    case"创意模块":
                        IderFragment iderFragment=new IderFragment();
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.id_content_view,iderFragment).commit();
                        break;




                }

            }
        });
        //显示和隐藏侧滑菜单
        mIvHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag){
                    mSlidingPaneLayout.closePane();
                    flag=false;
                }else {
                    mSlidingPaneLayout.openPane();
                    flag=true;
                }

            }
        });


    }
    //初始化代码
    private void initdata() {
        allCarPeccancieList=new ArrayList<>();
        carInfoList=new ArrayList<>();
        menuNameArray= getResources().getStringArray(R.array.menuarray);
        menuList=new ArrayList<>();
        for (int i=0;i<menuList.size();i++){
            Map map=new HashMap();
            map.put("name",menuNameArray);
            menuList.add(map);
        }


    }
    class  MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return menuList.size();
        }

        @Override
        public Object getItem(int position) {
            return menuList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=View.inflate(MainActivity.this,R.layout.menu_list_item,null);
            TextView mTvUserName=convertView.findViewById(R.id.id_tv_menu_name);
            mTvUserName.setText((String)menuList.get(position).get("name"));
            ImageView mIvImageView=convertView.findViewById(R.id.id_iv_menu_icon);
            mIvImageView.setImageResource(R.drawable.arrow);


            return convertView;
        }
    }
}
